package com.hp.idempotence.v3.service;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hp.idempotence.common.Result;
import com.hp.idempotence.v3.model.Goods;
import com.hp.idempotence.v3.repository.GoodsRepository;

@Service
public class GoodsService {
	
	@Autowired
	private GoodsRepository goodsRepository;

	@Transactional
	public Result decGood(long l) throws Exception {
		
		Goods goods = goodsRepository.getGoodAndLock(l);
		
		if(goods.getCount()<=0){
			throw new InterruptedException("改商品已卖完！");
		}
		
		//测试时间
		Thread.sleep(10000);
		
		goods.setCount(goods.getCount()-1);
		goodsRepository.save(goods);
		
		return Result.success("操作成功!");
	}

}
